var App = angular.module('app',['ngRoute']);
App.config(function($routeProvider){
	$routeProvider.when('/home',{templateUrl:"home.html",controller:"ct2"});
	$routeProvider.when('/list',{templateUrl:"list.html",controller:"ct3"});
	$routeProvider.when('/tonji',{template:"<h2>统计功能还在开发中</h2>"});
	$routeProvider.when('/my',{template:"<h2>我的功能还在开发中</h2>"});
	$routeProvider.otherwise({redirectTo:"home"});
})
App.service('crsj',function(){
	this.cr=function(dataObj){
		if(dataObj){
			dataStr = JSON.stringify(dataObj);
			localStorage.setItem('ly',dataStr);
		}else{
			localStorage.setItem('ly','{"info":[]}');
		}
	}
	this.qu=function(){
		dataStr = localStorage.getItem('ly');
		if(dataStr){
			dataObj = JSON.parse(dataStr);
			return dataObj
		}
	}
})
App.controller('ct1',['$scope','$rootScope',function($scope,$rootScope){
	//主控制器
}])
App.controller('ct2',['$scope','$rootScope',function($scope,$rootScope){
    if(localStorage.getItem('oa')){
    	var aD = localStorage.getItem('oa');
    	$scope.aa = JSON.parse(aD).name2;
    	$scope.ab = JSON.parse(aD).money2;
    	$scope.ac = JSON.parse(aD).timer;
    }else{
    	localStorage.setItem('oa','{"name2":0,"money2":0,"timer":0}');
    }
    $scope.xinZen=function(){
    	$(".shuXie2").css('display','block');
    }
    $scope.quxiao2=function(){
		$(".shuXie2").css('display','none');
	}
    $scope.add2=function(){
    	var od = localStorage.setItem('oa','{"name2":'+$scope.name2+',"money2":'+$scope.money2+',"timer":'+$scope.timer+'}');
    	var aD = localStorage.getItem('oa');
    	$scope.aa = JSON.parse(aD).name2;
    	$scope.ab = JSON.parse(aD).money2;
    	$scope.ac = JSON.parse(aD).timer;
    	$(".shuXie2").css('display','none');
    }
    if(localStorage.getItem('txt')){
    	$rootScope.tx = JSON.parse(localStorage.getItem('txt')).sum;
    }
}])
App.controller('ct3',['$scope','crsj','$rootScope',function($scope,crsj,$rootScope){
	//列表主控制器
	$scope.zd=function(){
    	$(".shuXie").css('display','block');
    }
	if(crsj.qu()){
		$scope.zhi=crsj.qu().info
	}else{
		crsj.cr()
	}
	if(localStorage.getItem('txt')){
		$rootScope.tx = JSON.parse(localStorage.getItem('txt')).sum;
	}else{
		localStorage.setItem('txt','{"sum":0}');
	}
	$scope.add=function(){
		dataObj =crsj.qu();
		dataObj.info.push({"name":$scope.name,"money":$scope.money,"time":$scope.time});
		$scope.zhi = dataObj.info;
//		$scope.time=new Date();
		crsj.cr(dataObj);
		$scope.sum=0;
		for(i of $scope.zhi){			
			$scope.sum+=parseInt(i.money);
    	}
		var otxt = localStorage.setItem('txt','{"sum":'+$scope.sum+'}');
		$rootScope.tx = JSON.parse(localStorage.getItem('txt')).sum;
		$(".shuXie").css('display','none');
	}
	$scope.remove=function(index){
		dataObj =crsj.qu();
		dataObj.info.splice(index,1);
		$scope.zhi = dataObj.info;
		crsj.cr(dataObj);		
	}
	$scope.quxiao=function(){
		$(".shuXie").css('display','none');
	}
}])